// pages/duanzi/duanzi.js
const util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   * {"content_type":3,"page":1,"page_size":10,"uid":"U5b1e8bc7d3ceb"}
   */
  data: {
    newList: {},
    param: {
      'content_type': '3',
      'page': 1,
      'page_size': 1,
      'uid': 'U5b1e8bc7d3ceb'
    },
    is_login: true, //是否登录  ,true登录
    login_text: '请登录',
    is_fabulous: false,
    id: '',
    all_support:0,
    is_video:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var id = options.id != undefined ? options.id : this.data.id;
    if (id) {
      this.setData({
        id:id
      })
      var url = app.url.duanzi_info;
      var param = {
        "id": "U5b1e8bc7d3ceb",
        "xh_id": id,
      }
      util.http(url, this.newResponse, param, 'POST')
    } else {
      var url = app.url.video;
      util.http(url, this.newResponse, this.data.param, 'POST')
    }
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (app.is_duanzi == false) {}
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    if (app.globalData.is_login == true) {
      var from_uid = wx.getStorageSync('uid');
    } else {
      var from_uid = '';
    }
    return {
      // title: this.data.newList.title,
      path: '/pages/duanzi/duanzi?id=' + this.data.newList.id + '&from_uid=' + from_uid,
    }
  },


  newResponse: function(res) {
    console.log(this.data.id,11)
    if(this.data.id){
      var newList = res.data.content.data
    }else{
      console.log(12312)
      var newList = res.data.content.data[0]
      console.log(newList);
    }
    this.setData({
      newList: newList,
      all_support: newList.support
    })
    setTimeout(
      function () {
        wx.hideLoading();
      },
    )
  },


  nextArticle: function() {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      id: ''
    })
    let rdm2 = Math.ceil(Math.random() * 10000)
    this.data.param.page = rdm2;
    var url = app.url.video;
    util.http(url, this.newResponse, this.data.param, 'POST')
  },

  //点赞
  fabulous: function(e) {
    if (e != undefined) {
      let id = e.currentTarget.dataset.id;
      this.setData({
        id: id,
      })
    } else {
      let id = this.data.id;
    }
    //登录
    if (app.globalData.is_login == false) {
      this.setData({
        is_login: app.globalData.is_login,
        is_fabulous: true,
        is_video:false,
      })
      return false
    }
    var source = e.currentTarget.dataset.source != undefined ? e.currentTarget.dataset.source:'';
    var title = e.currentTarget.dataset.title;
    var backstage_url = app.url.backstage + "user/fabulous";
    var a_id = this.data.id;
    var user_id = wx.getStorageSync('uid');
    var param = {
      'a_id': a_id,
      'user_id': user_id,
      'type': 2, //type 2为段子(写死)
      'title': title,
       'source': source,
    }
    util.http(backstage_url, this.addBackstage, param)
  },

  //点赞保存数据返回结果
  addBackstage: function(res) {
    console.log(res)
    if (res.data.errorCode == '00000') {
      var all_support =parseInt(this.data.all_support);
      this.setData({
        all_support: all_support+1,
      })
      wx.showToast({
        title: '赞+1',
        icon: 'succes',
        duration: 2000,
        image: '../../images/icon/like.png',
        mask: true
      })
    }
    if (res.data.errorCode == '10003') {
      wx.showToast({
        title: res.data.errorMsg,
        icon: 'none',
        duration: 2000
      })
    }
  },

  //登录
  login: function(userinfo) { // 需要一个参数来额外接收用户数据
    this.setData({
      is_login: app.globalData.is_login
    })
    if (userinfo.detail.errMsg == 'getUserInfo:ok') {
      this.setData({
        login_text: '正在登录中...'
      })
    }
    util.login((res) => {
      console.log(res, '回调')
      if (res.errMsg == 'request:ok') {
        app.globalData.is_login = true
        this.setData({
          is_login: true,
          is_video: true
        })
        this.onLoad(this);
        //当处理点赞时
        if (this.data.is_fabulous == true) {
          this.fabulous()
        }

      }
      if (userinfo.detail.errMsg == 'getUserInfo:fail auth deny') {
        //登录授权拒绝
        this.setData({
          is_login: true
        })
      }
    })
  },
})